<?php

/**

 * [WeEngine System] Copyright (c) 2014 WE7.CC

 * WeEngine is NOT a free software, it under the license terms, visited http://www.qincewang.com/ for more details.

 */$log = new PhpLog ( SDK_LOG_FILE_PATH, "PRC", SDK_LOG_LEVEL );



function coverParamsToString($params) {

	$sign_str = '';

		ksort ( $params );

	foreach ( $params as $key => $val ) {

		if ($key == 'signature') {

			continue;

		}

		$sign_str .= sprintf ( "%s=%s&", $key, $val );

			}

	return substr ( $sign_str, 0, strlen ( $sign_str ) - 1 );

}



function coverStringToArray($str) {

	$result = array ();



	if (! empty ( $str )) {

		$temp = preg_split ( '/&/', $str );

		if (! empty ( $temp )) {

			foreach ( $temp as $key => $val ) {

				$arr = preg_split ( '/=/', $val, 2 );

				if (! empty ( $arr )) {

					$k = $arr ['0'];

					$v = $arr ['1'];

					$result [$k] = $v;

				}

			}

		}

	}

	return $result;

}



function deal_params(&$params) {

	

	if (! empty ( $params ['customerInfo'] )) {

		$params ['customerInfo'] = base64_decode ( $params ['customerInfo'] );

	}

	

	if (! empty ( $params ['encoding'] ) && strtoupper ( $params ['encoding'] ) == 'utf-8') {

		foreach ( $params as $key => $val ) {

			$params [$key] = iconv ( 'utf-8', 'UTF-8', $val );

		}

	}

}





function deflate_file(&$params) {

	global $log;

	foreach ( $_FILES as $file ) {

		$log->LogInfo ( "---------处理文件---------" );

		if (file_exists ( $file ['tmp_name'] )) {

			$params ['fileName'] = $file ['name'];

			

			$file_content = file_get_contents ( $file ['tmp_name'] );

			$file_content_deflate = gzcompress ( $file_content );

			

			$params ['fileContent'] = base64_encode ( $file_content_deflate );

			$log->LogInfo ( "压缩后文件内容为>" . base64_encode ( $file_content_deflate ) );

		} else {

			$log->LogInfo ( ">>>>文件上传失败<<<<<" );

		}

	}

}





function deal_file($params) {

	global $log;

	if (isset ( $params ['fileContent'] )) {

		$log->LogInfo ( "---------处理后台报文返回的文件---------" );

		$fileContent = $params ['fileContent'];

		

		if (empty ( $fileContent )) {

			$log->LogInfo ( '文件内容为空' );

		} else {

						$content = gzuncompress ( base64_decode ( $fileContent ) );

			$root = SDK_FILE_DOWN_PATH;

			$filePath = null;

			if (empty ( $params ['fileName'] )) {

				$log->LogInfo ( "文件名为空" );

				$filePath = $root . $params ['merId'] . '_' . $params ['batchNo'] . '_' . $params ['txnTime'] . 'txt';

			} else {

				$filePath = $root . $params ['fileName'];

			}

			$handle = fopen ( $filePath, "w+" );

			if (! is_writable ( $filePath )) {

				$log->LogInfo ( "文件:" . $filePath . "不可写，请检查！" );

			} else {

				file_put_contents ( $filePath, $content );

				$log->LogInfo ( "文件位置 >:" . $filePath );

			}

			fclose ( $handle );

		}

	}

}





function create_html($params, $action) {

	$encodeType = isset ( $params ['encoding'] ) ? $params ['encoding'] : 'UTF-8';

	$html = <<<eot

<html>

<head>

    <meta http-equiv="Content-Type" content="text/html; charset={$encodeType}" />

</head>

<body  onload="javascript:document.pay_form.submit();">

    <form id="pay_form" name="pay_form" action="{$action}" method="post">

	

eot;

	foreach ( $params as $key => $value ) {

		$html .= "    <input type=\"hidden\" name=\"{$key}\" id=\"{$key}\" value=\"{$value}\" />\n";

	}

	$html .= <<<eot

    <input type="submit" type="hidden" value="稍等，支付跳转跳..." style="border:none;">

    </form>

</body>

</html>

eot;

	return $html;

}



?>